package com.example.myapplication.view;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.myapplication.databinding.FragmentMonthWeekBinding;

import java.util.Locale;

@FragmentName("月周日历切换")
public class MonthWeekFragment extends Fragment {
    private FragmentMonthWeekBinding binding;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        binding = FragmentMonthWeekBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        binding.calendar.setStretchCalendarEnable(false);
        binding.calendar.setMonthHoldEnable(true);
        binding.calendar.setWeekHoldEnable(true);
        binding.calendarStatus.setOnClickListener(v -> {
            ViewGroup.LayoutParams layoutParams = binding.calendar.getLayoutParams();
            if (binding.calendar.isWeekState()) {
                binding.calendar.toMonth();
                binding.calendarStatus.setText("收起");

                layoutParams.height = binding.calendar.getMonthHeight();
                binding.calendar.setLayoutParams(layoutParams);
            } else {
                binding.calendar.toWeek();
                binding.calendarStatus.setText("展开");

                layoutParams.height = binding.calendar.getWeekHeight();
            }
            binding.calendar.setLayoutParams(layoutParams);
        });
        binding.calendar.setOnCalendarChangedListener((baseCalendar, year, month, localDate, dateChangeBehavior) -> {
            binding.calendarDate.setText(localDate.toString());
            Log.i("calendar", "select_back: " + localDate);
        });

        binding.datePicker.setOnSelectedListener((year, monty, day) -> {
            binding.date.setText(String.format(Locale.getDefault(), "%d-%02d-%02d", year, monty, day));
        });
//        binding.datePicker.setDayVisible(false);
        binding.datePicker.setDateInfo("2023-04-21", "2026-09-26");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding.datePicker.onDestroy();
        binding = null;
    }
}
